<?php
require_once('JankParser.php');
/**
 * The JankController class is extended by any controllers created
 * in an application using JankFramework. Its main function is
 * to mediate the parameters performed on the application's model and
 * and displayed by its view (s). 
 * 
 * Every controller connects to a (extension of) JankParser, which can parse
 * templates in order to make up the view.
 * 
 * It can also connect to one or more models, though this is not required, 
 * and so not implemented in this base controller.
 *
 * @author Rick Hutcheson
 * @version 0.1
 */
abstract class JankController {
    
    /**
     *  @var string $template
     *   The string name of the full path to the template file to use with this 
     *   controller. This is a user-generated string, and so will be set to the 
     *   default template filename if the user input is invalid.
     */
    protected $template = "";
    
    /**
     * 
     * @var JankParser $parser
     *  A child class of JankParser, used to translate the application-specific
     *  templates to their rendered code.
     */
    protected $parser = NULL;
    
    /**
     * @var array $values
     *  An array of any values sent to the template in order to customize its 
     *  view.
     */
    public $values = array();
    
    
    /**
     * 
     * @var array $parameters
     *  An array of any parameters / parameters sent to the controller. These can 
     *  be used to display a different template file or perform an action
     *  on the models of the application.
     */
    protected $parameters = array();
    
    
    /**
     * Creates a new JankController, initialized with the name of the 
     * template which it will use for output.
     * 
     * @param array $parameters
     *  The parameters passed to this controller by the user, stored as an 
     *  associative array.
     */
    public function __construct($parameters) {
        $this->parameters  = $parameters;
        // If an action is specified for this controller, then we attempt to 
        // call the function with the same name as the action.
        if (!empty( $this->parameters) && 
        			method_exists($this, $this->parameters[0])) {    
        	$function = $this->parameters[0];
            // remove the function name from the list of action parameters. 
            array_shift($this->parameters);
            $this->$function();
        } else { 
            // no function is specified, so we call the default 'run' action.
            $this->run();
        }
    }
    
    /**
     * The run function is the default action for the controller. It is called 
     * if no action is specified. 
     */
    public abstract function run();
    
    public function display($page) {
    	require_once($this->templateDir . '/' . $page . '.php');
    }
}

